// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CBL_STATE_H__
#define __INCLUDED_CBL_STATE_H__

#include "CBL_Defines.h"
#include <vector>

CB_NAMESPACE_START

	class IState;

	// -----------------------------------------------------------------------------------------------------------------
	//	CConnection
	//	Defines a connection between two states. Overload for custom actions.
	//	Execute will be called after an OnLeave(NextState) on the current state was called.
	// -----------------------------------------------------------------------------------------------------------------
	
	class CConnection
	{
	public:
		
		CConnection(IState& _rNextState) : m_pNextState(&_rNextState) {}
		virtual void Execute() {}
		
		bool operator == (const IState& _rState) const				{ return m_pNextState == &_rState; }
		bool operator == (const CConnection& _rConnection) const	{ return m_pNextState == _rConnection.m_pNextState; }
		
	private:
		
		IState* m_pNextState;
	};

	// -----------------------------------------------------------------------------------------------------------------
	//	IState
	//	A single state. If this state is part of a statemachine execute will be called each time the state machine is
	//	executed, as long as this state is active.
	//	OnLeave will be called if a state change is about to happen.
	//	You have to add connections to allow changes to other states inside a state machine.
	// -----------------------------------------------------------------------------------------------------------------

	class IState
	{
	public:
		
		virtual ~IState() {}
		
		virtual IState* Execute() = 0;
		
		virtual void OnEnter(IState& _rPrevState) = 0;
		virtual void OnLeave(IState& _rNextState) = 0;
		
	public:
		
		BASELIB_API CConnection* GetConnection(const IState& _rNextState);
		
		BASELIB_API void AddConnection(CConnection& _rConnection);
		BASELIB_API void RemoveConnection(const IState& _rState);
		
	protected:
		
		typedef std::vector<CConnection*> CConnectionList;
		CConnectionList m_Connections;
	};

	// -----------------------------------------------------------------------------------------------------------------
	//	CStateMachine
	//	Container for states. Do call Execute() frequently or at least after a ChangeState() call, as a state will only
	//	change during the next Execute() call after a ChangeState().
	//	State machines are states and can thus be added to a statemachine themselves.
	// -----------------------------------------------------------------------------------------------------------------

	class CStateMachine : public IState
	{
	public:
		
		CStateMachine(IState& _rFirstState)
		: m_pActiveState(&_rFirstState)
		{}
		
		void ChangeState(IState& _rState)	{ m_pNextState = &_rState; }
		IState& GetCurrentState() const		{ return *m_pActiveState; }
		
	public:
		
		BASELIB_API virtual IState* Execute();
		
	protected:
		
		IState*	m_pActiveState;
		IState*	m_pNextState;
	};

CB_NAMESPACE_END

#endif